//
//  CollectionViewController.m
//  CommonFrame
//
//  Created by Bang-guo on 15/7/28.
//  Copyright (c) 2015年 guobang. All rights reserved.
//

#import "CollectionViewController.h"

@interface CollectionViewController ()

@end

@implementation CollectionViewController
-(void)viewWillAppear:(BOOL)animated {
    
    [super viewWillAppear:animated];
    [self.collectionTableView reloadData];
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationItem.title = @"收藏详情";
    
    
    self.collectionTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT - 113) style:UITableViewStylePlain];
    self.collectionTableView.delegate = self;
    self.collectionTableView.dataSource = self;
    [self.view addSubview:self.collectionTableView];


    
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    NearbyDetailCDM *near = [NSMutableArray arrayWithArray:[[CoreDataHandle shareCoreData]  selectAll:@"NearbyDetailCDM"]][indexPath.row];
    NearbyDetailViewController *nearbyDVC = [[NearbyDetailViewController alloc] init];
    nearbyDVC.placeId = near.placeId;
    [self.navigationController pushViewController:nearbyDVC animated:YES];
    
    
}

-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    return YES;
}

///设置编辑类型:删除或者插入
-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    return UITableViewCellEditingStyleDelete;
}


-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (editingStyle==UITableViewCellEditingStyleDelete) {
        
        NearbyDetailCDM *near = [NSMutableArray arrayWithArray:[[CoreDataHandle shareCoreData]  selectAll:@"NearbyDetailCDM"]][indexPath.row];
        
        [[CoreDataHandle shareCoreData]deleteData:@"NearbyDetailCDM" withKey:@"placeId" withValue:near.placeId];

        

        [self.collectionTableView reloadData];

    }
    
    
}
-(NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    return @"删除";
}






-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 1;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *identifier = @"cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    NearbyDetailCDM *near = [NSMutableArray arrayWithArray:[[CoreDataHandle shareCoreData]  selectAll:@"NearbyDetailCDM"]][indexPath.row];
    cell.textLabel.text = near.name;

    return cell;
    
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    
    return [NSMutableArray arrayWithArray:[[CoreDataHandle shareCoreData]  selectAll:@"NearbyDetailCDM"]].count;
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
